package com.parmal.proxy.safe;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.keyvalue.core.KeyValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Scope("singleton")
public class UserValidator {

    private RedisTemplate<String, String> redisTemplate;

    private ValueOperations<String, String> valueOperations;

    @Autowired
    public UserValidator(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
        this.valueOperations = this.redisTemplate.opsForValue();
    }

    /**
     * 验证用户名密码是否有效
     * @param userName
     * @param password
     * @return
     */
    public boolean validate(String userName, String password) {
        return this.valueOperations.get(userName).equals(password);
    }

}
